<?php

namespace Database\Seeders;

use App\Models\System\User;
use App\Models\System\Menus;
use Illuminate\Support\Facades\File;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;

class UserSeeder extends Seeder
{
    use WithoutModelEvents;

    const GUARD_NAME = 'admin';
    private $role;
    private array $menusData = [
        [
            'name' => 'content.manage',
            'display_name' => '内容管理',
            'path' => '/content',
            'icon' => 'el-icon-reading',
            'genre' => 1,
            'child' => [
                [
                    'name' => 'content.kv',
                    'display_name' => '广告管理',
                    'path' => '/content/kv',
                    'component' => 'content/kv',
                    'icon' => 'el-icon-view',
                    'genre' => 1,
                    'child' => [
                        ['name' => 'content.kv.create', 'display_name' => '添加广告', 'genre' => 2],
                        ['name' => 'content.kv.edit', 'display_name' => '编辑广告', 'genre' => 2],
                        ['name' => 'content.kv.destroy', 'display_name' => '删除广告', 'genre' => 2],
                    ]
                ],
                [
                    'name' => 'content.articles',
                    'display_name' => '文章管理',
                    'path' => '/content/articles',
                    'component' => 'content/articles',
                    'icon' => 'el-icon-tickets',
                    'genre' => 1,
                    'child' => [
                        ['name' => 'content.articles.create', 'display_name' => '添加文章', 'genre' => 2],
                        ['name' => 'content.articles.edit', 'display_name' => '编辑文章', 'genre' => 2],
                        ['name' => 'content.articles.destroy', 'display_name' => '删除文章', 'genre' => 2],
                        ['name' => 'content.articles.recover', 'display_name' => '回收站恢复', 'genre' => 2],
                        ['name' => 'content.articles.expurgate', 'display_name' => '回收站删除', 'genre' => 2],
                    ]
                ],
                [
                    'name' => 'content.column',
                    'display_name' => '栏目管理',
                    'path' => '/content/column',
                    'component' => 'content/column',
                    'icon' => 'el-icon-fold',
                    'genre' => 1,
                    'child' => [
                        ['name' => 'content.column.create', 'display_name' => '添加栏目', 'genre' => 2],
                        ['name' => 'content.column.edit', 'display_name' => '编辑栏目', 'genre' => 2],
                        ['name' => 'content.column.destroy', 'display_name' => '删除栏目', 'genre' => 2],
                    ]
                ],
                [
                    'name' => 'content.tags',
                    'display_name' => '标签管理',
                    'path' => '/content/tags',
                    'component' => 'content/tags',
                    'icon' => 'el-icon-collection-tag',
                    'genre' => 1,
                    'child' => [
                        ['name' => 'content.tags.create', 'display_name' => '添加栏目', 'genre' => 2],
                        ['name' => 'content.tags.edit', 'display_name' => '编辑栏目', 'genre' => 2],
                        ['name' => 'content.tags.destroy', 'display_name' => '删除栏目', 'genre' => 2],
                    ]
                ],
                [
                    'name' => 'content.nav',
                    'display_name' => '导航栏管理',
                    'path' => '/content/nav',
                    'component' => 'content/nav',
                    'icon' => 'el-icon-menu',
                    'genre' => 1,
                    'child' => [
                        ['name' => 'content.nav.create', 'display_name' => '添加导航栏', 'genre' => 2],
                        ['name' => 'content.nav.edit', 'display_name' => '编辑导航栏', 'genre' => 2],
                        ['name' => 'content.nav.destroy', 'display_name' => '删除导航栏', 'genre' => 2],
                    ]
                ],
                [
                    'name' => 'content.files',
                    'display_name' => '附件管理',
                    'path' => '/content/files',
                    'component' => 'content/files',
                    'icon' => 'el-icon-folder',
                    'genre' => 1,
                    'child' => [
                        ['name' => 'content.filesGroup.create', 'display_name' => '添加分组', 'genre' => 2],
                        ['name' => 'content.filesGroup.edit', 'display_name' => '编辑分组', 'genre' => 2],
                        ['name' => 'content.filesGroup.destroy', 'display_name' => '删除分组', 'genre' => 2],

                        ['name' => 'content.files.upload', 'display_name' => '上传附件', 'genre' => 2],
                        ['name' => 'content.files.destroy', 'display_name' => '删除附件', 'genre' => 2],
                        ['name' => 'content.files.recycle', 'display_name' => '回收站', 'genre' => 2],
                        ['name' => 'content.files.recover', 'display_name' => '回收站恢复', 'genre' => 2],
                        ['name' => 'content.files.expurgate', 'display_name' => '回收站删除', 'genre' => 2],
                    ]
                ]
            ]
        ],
        [
            'name' => 'system.manage',
            'display_name' => '系统管理',
            'path' => '/system',
            'icon' => 'el-icon-setting',
            'genre' => 1,
            'child' => [
                [
                    'name' => 'system.user',
                    'display_name' => '账号管理',
                    'path' => '/system/user',
                    'component' => 'system/user',
                    'icon' => 'el-icon-user-filled',
                    'genre' => 1,
                    'child' => [
                        ['name' => 'system.user.create', 'display_name' => '添加账号', 'genre' => 2],
                        ['name' => 'system.user.edit', 'display_name' => '编辑账号', 'genre' => 2],
                        ['name' => 'system.user.destroy', 'display_name' => '删除账号', 'genre' => 2],
                        ['name' => 'system.user.resetPassword', 'display_name' => '重置密码', 'genre' => 2],
                    ]
                ],
                [
                    'name' => 'system.role',
                    'display_name' => '角色管理',
                    'path' => '/system/role',
                    'component' => 'system/role',
                    'icon' => 'el-icon-notebook',
                    'genre' => 1,
                    'child' => [
                        ['name' => 'system.role.create', 'display_name' => '添加角色', 'genre' => 2],
                        ['name' => 'system.role.edit', 'display_name' => '编辑角色', 'genre' => 2],
                        ['name' => 'system.role.destroy', 'display_name' => '删除角色', 'genre' => 2],
                        ['name' => 'system.role.permission', 'display_name' => '分配权限', 'genre' => 2],
                    ]
                ],
                [
                    'name' => 'system.menus',
                    'display_name' => '菜单管理',
                    'path' => '/system/menus',
                    'component' => 'system/menus',
                    'icon' => 'el-icon-fold',
                    'genre' => 1,
                    'child' => [
                        ['name' => 'system.menus.create', 'display_name' => '添加菜单', 'genre' => 2],
                        ['name' => 'system.menus.edit', 'display_name' => '编辑菜单', 'genre' => 2],
                        ['name' => 'system.menus.destroy', 'display_name' => '删除菜单', 'genre' => 2],
                    ]
                ],
                [
                    'name' => 'system.dict',
                    'display_name' => '字典管理',
                    'path' => '/system/dict',
                    'component' => 'system/dict',
                    'icon' => 'el-icon-document',
                    'genre' => 1,
                    'child' => [
                        ['name' => 'system.dictGroup.create', 'display_name' => '添加字典分类', 'genre' => 2],
                        ['name' => 'system.dictGroup.edit', 'display_name' => '编辑字典分类', 'genre' => 2],
                        ['name' => 'system.dictGroup.destroy', 'display_name' => '删除字典分类', 'genre' => 2],

                        ['name' => 'system.dict.create', 'display_name' => '添加字典项', 'genre' => 2],
                        ['name' => 'system.dict.edit', 'display_name' => '编辑字典项', 'genre' => 2],
                        ['name' => 'system.dict.destroy', 'display_name' => '删除字典项', 'genre' => 2],
                    ]
                ],
                [
                    'name' => 'system.config',
                    'display_name' => '系统设置',
                    'path' => '/system/config',
                    'component' => 'system/config',
                    'icon' => 'el-icon-tools',
                    'genre' => 1,
                    'child' => [
                        ['name' => 'system.config.update', 'display_name' => '更新设置', 'genre' => 2],
                    ]
                ],
            ]
        ],
        [
            'name' => 'maintain.manage',
            'display_name' => '系统监控',
            'path' => '/maintain',
            'icon' => 'el-icon-aim',
            'genre' => 1,
            'child' => [
                [
                    'name' => 'maintain.database',
                    'display_name' => '数据表维护',
                    'path' => '/maintain/database',
                    'component' => 'maintain/database',
                    'icon' => 'el-icon-grid',
                    'genre' => 1,
                    'child' => [
                        ['name' => 'maintain.database.optimize', 'display_name' => '优化表', 'genre' => 2],
                        ['name' => 'maintain.database.repair', 'display_name' => '修复表', 'genre' => 2],
                        ['name' => 'maintain.database.clear', 'display_name' => '清空表', 'genre' => 2],
                    ]
                ],
                [
                    'name' => 'maintain.logs',
                    'display_name' => '日志监控',
                    'path' => '/maintain/logs',
                    'icon' => 'el-icon-calendar',
                    'genre' => 1,
                    'child' => [
                        [
                            'name' => 'maintain.logs.login',
                            'display_name' => '登陆日志',
                            'path' => '/maintain/logs/login',
                            'component' => 'maintain/logs/login',
                            'icon' => 'el-icon-reading',
                            'genre' => 1,
                        ],
                        [
                            'name' => 'maintain.logs.activity',
                            'display_name' => '操作日志',
                            'path' => '/maintain/logs/activity',
                            'component' => 'maintain/logs/activity',
                            'icon' => 'el-icon-document',
                            'genre' => 1,
                        ],
                        [
                            'name' => 'maintain.logs.access',
                            'display_name' => '访问日志',
                            'path' => '/maintain/logs/access',
                            'component' => 'maintain/logs/access',
                            'icon' => 'el-icon-view',
                            'genre' => 1,
                        ],
                        [
                            'name' => 'maintain.logs.error',
                            'display_name' => '错误日志',
                            'path' => '/maintain/logs/error',
                            'component' => 'maintain/logs/error',
                            'icon' => 'el-icon-warning',
                            'genre' => 1,
                        ],
                    ]
                ],
            ]
        ],
    ];

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $this->role = Role::create([
            'name' => 'root',
            'guard_name' => self::GUARD_NAME
        ]);
        $this->dealWitchMenus($this->menusData);
        $data = $this->getFileData(storage_path('app/.pwd'));
        $user = User::create([
            'avatar' => 'static/admin/img/default_heading.gif',
            'username' => $data['username'],
            'display_name' => '超级管理员',
            'password' => bcrypt($data['password']),
            'status' => 1,
            'uuid' => Str::uuid()
        ]);
        $user->assignRole($this->role);
        echo $this->arrToString($data);
    }

    private function getFileData($path): array
    {
        $newArr = [
            'username' => $fileData['username'] ?? 'root',
            'password' => $fileData['password'] ?? Str::random(12),
        ];
        if (File::exists($path)) {
            $data = File::get($path);
            $arr = explode("\n", $data);
            if (!empty($arr)) {
                foreach ($arr as $value) {
                    if (!empty($value)) {
                        [$k, $v] = explode(':', $value);
                        $newArr[$k] = trim($v);
                    }
                }
            }
            return $newArr;
        }
        File::put($path, $this->arrToString($newArr));
        return $newArr;
    }

    private function arrToString(array $arr): string
    {
        $str = '';
        foreach ($arr as $ke => $va) {
            $str .= $ke.': '.$va."\n";
        }
        return $str;
    }

    private function dealWitchMenus(array $permissions, int $parentId = 0): void
    {
        if (!empty($permissions)) {
            foreach ($permissions as $pem) {
                $menu = $this->createMenus($pem, $parentId);
                if (!empty($pem['child'])) {
                    $this->dealWitchMenus($pem['child'], $menu->id);
                }
            }
        }
    }

    private function createMenus(array $data, int $parentId)
    {
        $this->createPermission($data);
        return Menus::create([
            'name' => $data['name'],
            'display_name' => $data['display_name'],
            'path' => $data['path'] ?? '',
            'component' => $data['component'] ?? '',
            'icon' => $data['icon'] ?? '',
            'genre' => $data['genre'],
            'parent_id' => $parentId,
            'status' => 1,
        ]);
    }

    private function createPermission(array $data): void
    {
        $permission = Permission::create(['name' => $data['name'], 'guard_name' => self::GUARD_NAME]);
        $this->role->givePermissionTo($permission);
    }
}
